package cc.ruih.meizanchen.mm;

import java.util.Calendar;
import java.util.Date;

import cc.ruih.meizanchen.R;
import cc.ruih.meizanchen.Setting;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 * 妈妈版首页
 * @author 郭瑞坡
 *
 */
public class MMIndex extends Activity {

	ImageButton index1;
	ImageButton index2;
	ImageButton index3;
	ImageButton index4;
	ImageButton index5;
	ImageButton setting;	// 跳转到设置页面
	private ProgressBar myProgressBar;
	
	TextView call;
	TextView weeks;
	TextView days;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mm_index);
		TextView headTitle=(TextView) findViewById(R.id.headTitle);
		headTitle.setVisibility(View.INVISIBLE);
		index1 = (ImageButton) findViewById(R.id.mm_index1);
		index2 = (ImageButton) findViewById(R.id.mm_index2);
		index3 = (ImageButton) findViewById(R.id.mm_index3);
		index4 = (ImageButton) findViewById(R.id.mm_index4);
		index5 = (ImageButton) findViewById(R.id.mm_index5);
		setting = (ImageButton) findViewById(R.id.setting);
		index1.setOnClickListener(new ButtonClick());
		index2.setOnClickListener(new ButtonClick());
		index3.setOnClickListener(new ButtonClick());
		index4.setOnClickListener(new ButtonClick());
		index5.setOnClickListener(new ButtonClick());
		setting.setOnClickListener(new ButtonClick());
		
		call = (TextView) findViewById(R.id.call);		// 称呼
		weeks = (TextView) findViewById(R.id.weeks);		// 怀孕N周N天
		days = (TextView) findViewById(R.id.days);		// 尚余N天
		
		SharedPreferences sharedPreferences = getSharedPreferences("mminfo", Context.MODE_PRIVATE);
		String motherName = sharedPreferences.getString("name", "");
		String yuchanqi = sharedPreferences.getString("ycq", "");
		
		call.setText(motherName+"，您好");
		formatDate(yuchanqi);
		myProgressBar = (ProgressBar) findViewById(R.id.progress);		// 进度条
	}
	
	// 格式化求出预产期
	public void formatDate(String str){
		int year, month, day;		// 定义年、月、日
		Date ycqDate = new Date();		// 定义预产期
		Date todayDate = new Date();	// 定义今日日期
		int lcts;		// 定义临产天数
		
		String[] strs=str.split("年");
		year=Integer.parseInt(strs[0]);
		String[] strs2=strs[1].split("月");
		month=Integer.parseInt(strs2[0]);
		String[] strs3=strs2[1].split("日");
		day=Integer.parseInt(strs3[0]);
		ycqDate.setYear(year);
		ycqDate.setMonth(month);
		ycqDate.setDate(day);
		
		// 求出今日信息
		final Calendar c = Calendar.getInstance(); 
		todayDate.setYear(c.get(Calendar.YEAR));
		todayDate.setMonth(c.get(Calendar.MONTH));
		todayDate.setDate(c.get(Calendar.DAY_OF_MONTH));
		
		lcts = getDays(todayDate, ycqDate);
		days.setText("尚余"+lcts+"天");
		if((280-lcts)%7==0){
			weeks.setText("第"+((280-lcts)/7+1)+"周第"+7+"天");
		}else{
			weeks.setText("第"+((280-lcts)/7+1)+"周第"+(280-lcts)%7+"天");
		}
		setProgress((280-lcts)/280*100);		// 进度条目前百分比位置
	}
	
	// 求临产天数
	private int getDays(Date today, Date birth){
		return (int) ((birth.getTime() - today.getTime()) / (3600L * 1000 * 24));
	}

	private class ButtonClick implements OnClickListener {
		Intent intent = new Intent();
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.mm_index1:
				intent.setClass(MMIndex.this, MMMain.class);
				startActivity(intent);
				break;
			/*case R.id.mm_index2:
				intent.setClass(MMIndex.this, AntenatalNotice.class);
				break;
			case R.id.mm_index3:
				//intent.setClass(MMIndex.this, Chanshi.class);
				break;
			case R.id.mm_index4:
				//intent.setClass(MMIndex.this, HaoyunTool.class);
				break;
			case R.id.mm_index5:
				intent.setClass(MMIndex.this, More.class);
				break;
			//输入指标页面
			case R.id.mm_index5:
				//intent.setClass(MMIndex.this, More.class);
				break;*/
			case R.id.setting:
				intent.setClass(MMIndex.this, Setting.class);
				startActivity(intent);
				break;
			}
			
		}
	}
}